﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 * 目标标本：4.0.30319.239
 * 
 * 最后修改：2011/11/9 8:19:33
 * ************************************************************/
namespace Mozlite.Web
{
    using System;
    using System.Linq;
    using System.Text;
    using System.Web.Mvc;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using Mozlite.Common.Configuration;
    using Mozlite.Common;
    using System.Text.RegularExpressions;

    /// <summary>
    /// 验证密码属性。
    /// </summary>
    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
    public class ValidatePasswordAttribute : ValidationAttribute, IClientValidatable
    {
        /// <summary>
        /// 初始化类<see cref="T:Mozlite.WebSite.Models.ValidatePasswordAttribute"/>。
        /// </summary>
        public ValidatePasswordAttribute() : base(Resources.GetString(CreateUserStatus.InvalidPassword) + SiteSettings.Current.PasswordRegexDescription) { }
        /// <summary>
        /// 判断是否通过验证。
        /// </summary>
        /// <param name="value">验证的实例对象。</param>
        /// <returns>返回是否通过验证。</returns>
        public override bool IsValid(object value)
        {
            return SiteSettings.IsPasswordValid(value as string);
        }
        /// <summary>
        /// 在类中实现时，返回该类的客户端验证规则。
        /// </summary>
        /// <param name="metadata">模型元数据。</param>
        /// <param name="context">控制器上下文。</param>
        /// <returns>此验证程序的客户端验证规则。</returns>
        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            return new[] { 
                new ModelClientValidationRegexRule(SiteSettings.Current.PasswordRegexDescription, SiteSettings.Current.PasswordRegex)
            };
        }
    }
}